<?php namespace Common\Model;
use Hdphp\Model\Model;
//商品表
class Goods extends Model{
	protected $table='goods';
	//自动验证
	protected $validate=array(
		array('gname','required','商品名称不得为空',3,3),
		array('shopprice','required','商城价格不能为空',3,3),
		array('stock','required','商品库存不能为空',3,3),	
	);	
	protected $auto=array(
		array('uploadtime','time','function',3,3),
		array('gorder','gorder','method',3,3),	
	);

	protected function gorder(){
		$var=rand(1111111, 2132132);		
		return $var;
	}
	//声明自定义store添加函数
	public function store(){
	    //完成自动验证
	    if(!$this->create()) return FALSE;
		//分类必须选择，品牌必须选择
		if(Q('post.cate_id',0,'intval')==0){
			$this->error='分类必须选择';
			return FALSE;
		}
		
		//判断商品的列表图不能为空
		$a=Q('post.listimg');
		if(empty($a)){
			$this->error='列表图不能为空';
			return FALSE;
		}
		//实例化两个模型。Gooddetail和Goodsattr
		$gdModel=new \Common\Model\Gooddetail;
		$gaModel=new \Common\Model\Goodsattr;
		//1.判断两张表必须添加
		if(!$gdModel->create()){
			$this->error=$gdModel->geterror();
			return FALSE;
		}
		//商品添加	
		$gid=$this->add();	
		$gdModel->data['gdimg']=implode(',', Q('post.gdimg'));
		//商品详细的添加
		$gdModel->data['goods_gid']=$gid;
		$gdModel->add();
		//添加到商品属性表表里去
		//1。对固定属性的添加
		$arr=is_null(Q('post.attr'))?array():Q('post.attr');		
		foreach ($arr as $k => $v) {
			if(!empty($v)){
				$gaModel->data['typeattr_taid']=$k;
				$gaModel->data['gavalue']=$v;
				$gaModel->data['goods_gid']=$gid;
				$gaModel->add();
			}			
		}
		//2.对规格的添加
		$spec=is_null(Q('post.spec'))?array():Q('post.spec');
		foreach ($spec as $k=> $v) {
			//1.第一层循环是对类型属性的循环
			foreach ($v['value'] as $ke => $va) {
			//2.第二层循是对value单个的循环
			//判断是 不是value值为空
				if($va!=''){
					$gaModel->data['typeattr_taid']=$k;
					$gaModel->data['gavalue']=$va;	
					$gaModel->data['gaadd']=$v['price'][$ke];
					$gaModel->data['goods_gid']=$gid;					
					$gaModel->add();
				}
								
			}						
		}
	
		return true;
	}

	//模型的edit方法,对商品的编辑
	public function edit(){
//		p(Q('post.'));	exit;
		$gid=Q('get.gid',0,'intval');		
	  //首先完成自动验证 
	  if(!$this->create()) return false;
	  //验证列表图是不是为空
	  if(is_null(Q('post.listimg'))||is_null(Q('post.gdimg'))){
	  	$this->error='列表图或商品图册不能为空';
		  return false;
	  }
	//验证商品详情描述必须填
	$b=Q('post.gddetail');
		if(empty($b))
		{
			$this->error='商品详细不得为空';
			return false;
		}
	//验证完成之后就开始对数据库的对应的表的修改
	//1.商品表的修改
		$this->save();		
	//2.商品详情的添加		
		$gdetailModel=new \Common\Model\Gooddetail;		
		//1)手动吧对应的商品属性需要的字段压入到data里面去
		$arr=array(
			'gdid'=>Q('post.gdid',0,'intval'),
			'gdimg'=>implode(',', Q('post.gdimg')),
			'gddetail'=>Q('post.gddetail'),
			'gdsell'=>Q('post.gdsell',''),
			'goods_gid'=>$gid,
		);
		$gdetailModel->save($arr);
	//3.对商品属性表的修改
		//1).在修改之前先将所有对应的商品id的属性和规格都删掉。这样就不用获得每个需要修改的主键啦
			$goodattrModel=new \Common\Model\Goodsattr;
			$data=$goodattrModel->where('goods_gid='.$gid)->get();			
			$goodattrModel->where('goods_gid='.$gid)->delete();	
			//同时删除掉对应商品的货品内容
			Db::table('productlist')->where('goods_gid='.$gid)->delete();
		//2).组合数组添加现将对应的attr属性添加了。然后在添加spec		
		if(!is_null(Q('post.attr'))){
			
			foreach (Q('post.attr') as $k => $v) {
				if($v!='0'){
					$arr=array(
						'gavalue'=>$v,
						'typeattr_taid'=>$k,
						'goods_gid'=>$gid,
					);			
				
					$goodattrModel->add($arr);
				}				
			}
		}			
			
		//3).对spec的添加
		//判断是否有spec是否存在
		if(!is_null(Q('post.spec'))){
			foreach (Q('post.spec') as $k => $v) {
				//判断$v['value']里面的值是不是一个等
			
				foreach ($v['value'] as $ke => $va) {
					//判断是不是有用的数据
					if($va!='0'){
						$goodattrModel->data['gavalue']=$va;
						$goodattrModel->data['gaadd']=$v['price'][$ke];
						$goodattrModel->data['goods_gid']=$gid;
						$goodattrModel->data['typeattr_taid']=$k;
						$goodattrModel->add();
					}
				}
			}
			
			
		}

		return true;
	}
	
}
?>